package com.ww.learn.oop.sam

/**
 * SAM接口的组合
 */

fun interface Transformer<T, R> {
    fun transform(t: T): R
}

// 组合多个转换器
fun <T, R, U> Transformer<T, R>.andThen(next: Transformer<R, U>): Transformer<T, U> {
    return Transformer { t -> next.transform(this.transform(t)) }
}

fun main() {
    val toUpper = Transformer<String, String> { it.uppercase() }
    val addExclamation = Transformer<String, String> { "$it!" }

    val combined = toUpper.andThen(addExclamation)

    println(combined.transform("hello"))
}